import { Component, OnInit } from '@angular/core';
import { CakeOrderViewComponent } from '../cake-order/order-view/cake-order-view.component';
import { myValidNull } from '@core/util/string-utils';
import { Router } from '@angular/router';
import { MyModalHelper } from '@core/my-modal.helper';
import { OrderLogisticsComponent } from '../../order-logistics/order-logistics.component';
import { NzMessageService } from 'ng-zorro-antd';
import { CakePostalService } from '@core/routes-services/cake-postal.service';
import { SalesmanService } from '@core/routes-services/salesman.service';
import { ActivityProductService } from '@core/routes-services/activity-product.service';
import { MARKET_SALESMAN_ROLE } from '../../../constant/dictionary';

@Component({
  selector: 'app-mid-autumn-cake-postal',
  templateUrl: './cake-postal.component.html',
})
export class MidAutumnCakePostalComponent implements OnInit {

  isLoading = false;
  keyword: any;
  status:any;
  salesmanId:any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  statusOptions: any [] =[
    {
      value: '待发货',
      key: 1,
    },
    {
      value: '已发货',
      key: 2,
    },
  ];

  //salesman角色: 1-业务员，2-渠道商，3-门店，4-代理
  salesmanRoleOptions: any [] =  MARKET_SALESMAN_ROLE;

  allSalesmans:any [] = [];
  allProducts:any [] = [];
  constructor(private message: NzMessageService,
              private router: Router,
              private cakePostalService: CakePostalService,
              private salesmanService: SalesmanService,
              private activityProductService:ActivityProductService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
    this.initOptions();
  }

  initOptions(){
    this.salesmanService.getAll().then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.allSalesmans = res.data.marketSalesmanList;
      } else {
        this.message.warning(res.message);
      }
    }, error => {
      this.message.error(error.message ? error.message : error.error)
    });
    this.activityProductService.getAll().then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.allProducts = res.data.activityProductsList;
      } else {
        this.message.warning(res.message);
      }
    }, error => {
      this.message.error(error.message ? error.message : error.error)
    });
  }
  initPage() {
    this.isLoading = true;
    this.cakePostalService.pagination(this.page, this.keyword, this.status, this.salesmanId).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  seeEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(CakeOrderViewComponent, { entity: bak ,viewType: 2,statusOption:this.statusOptions,allProducts:this.allProducts}, 'lg', {
      nzTitle: '订单信息',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  logistics = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(OrderLogisticsComponent, { entity: bak, businessService: this.cakePostalService }, 520, {
      nzTitle: '物流信息',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };


}
